package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 预约信息Service业务层处理
 * 
 * @author elina
 * @date 2025-10-15
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 获取当天取消预约次数
     * @return
     */
    @Override
    public Long getCancelReservationCount() {
        Long userId = UserThreadLocal.get();
        if (ObjectUtil.isNull(userId)) {
            throw new BaseException("登录信息异常！");
        }

        // 获取当天的开始时间以及结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return lambdaQuery().eq(Reservation::getCreateBy, userId)
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getCreateTime, startTime, endTime)
                .count();
    }

    /**
     * 获取每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<Map<String, Object>> getReservationCount(Long time) {
        if (ObjectUtil.isNull(time)) {
            throw new BaseException("预约时间段为空！");
        }
        LocalDateTime startTime = LocalDateTimeUtil.of(time);
        // 在上面的时间上加半小时
        LocalDateTime endTime = startTime.plusMinutes(30);
        List<Map<String, Object>> result = reservationMapper.getReservationCount(startTime, endTime);

        return result;
    }

                                    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return reservationMapper.deleteById(id);
    }
}
